var slist;
var ws, aid, type;//医馆id；药品类型：2：中药，109：西药
document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	aid = ws.aid;
	type = ws.type;
	console.log(aid+" "+type)
	showWating();
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=medicinal&a=shopMedicinal",
		data :{
			aid :aid,
			type :type
		},
		dataType :'json',
		success : function(data){
			plus.nativeUI.closeWaiting();
			slist = data;
			if(data.length > 0){
				var list = '',str='';
				$.each(data, function(k,v) {
					list += '<a class="mui-control-item classtype" data-tid="'+v.tid+'">'+v.classname+'</a>';
					$.each(v.product, function(key,value) {
						str = '<div onclick=goinfo("'+value.id+'")>'+
								'<img src="'+webRoot+value.pro+'" alt="" />'+	
								'<span>'+(value.title).substring(0,2)+'</span>'+
							'</div>';
						$('.goods').append(str);
					})
				});
				$('#segmentedControls').html(list);
			}
			$('.classtype').on('tap' , function(){
				$(this).addClass('mui-active');
				$(this).nextAll().removeClass('mui-active');
				$(this).prevAll().removeClass('mui-active');
				item($(this).attr('data-tid')); 
				$('#content1').show();
			});
		},
		error : function(e){
			console.log(JSON.stringify(e));
		}
	});
	//搜索
	$('.search').unbind().on('change', function() {
		plus.nativeUI.showWaiting();
		var search = $('.search').val();
		$.ajax({
			type:"get",
			url:apiRoot+"?m=home&c=medicinal&a=searchM&search="+search+"&aid="+aid+"&type="+type,
			dataType :'json',
			success :function(data){
				plus.nativeUI.closeWaiting();
				console.log(JSON.stringify(data));
				if(data.length > 0){
					var str = '';
					$.each(data, function(k,v) {
						str += '<div onclick=goinfo("'+v.id+'")>'+
								'<img src="'+webRoot+v.pro+'" alt="" />'+	
								'<span>'+(v.title).substring(0,2)+'</span>'+
							'</div>';
					});
					$('.goods').html(str);
					$('.classtype').removeClass('mui-active');
				}else if(search.trim()){
					$('.goods').html('');
				}
			},
			error :function(e){
				plus.nativeUI.closeWaiting();
				console.log(JSON.stringify(e));
				errortoast();
			}
		});
	})
},false);
//加载商城分类商品
function item(tid){
	if(slist.length > 0){
		$.each(slist, function(k,v) {
			if(v.tid == tid){
				$('.classpic').attr('src',webRoot + v.litpic);
				var str = '';
				$.each(v.product, function(key, value) {
					str += '<div onclick=goinfo("'+value.id+'")>'+
								'<img src="'+webRoot+value.pro+'" alt="" />'+	
								'<span>'+(value.title).substring(0,2)+'</span>'+
							'</div>';
				});
				$('.goods').html(str);
			}
		});
	}
}
function goinfo(aid){
	goNewPage('shop-information.html',{aid:aid, type:type})
}
